Android: Take picture with uri










0















I am trying to take a picture in Android using an Uri. But I am struggeling getting the bitmap from the created Uri. I always get a null-Object from my Uri.



Uri imgUri;
File newfile;
public static Button camButton;
public static ImageView img;
public static TextView text;

public void photo(Button cB, ImageView im, TextView tv)

text = tv;
camButton = cB;
img = im;

camButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
String file = dir + "test" + ".png";
newfile = new File(file);
try
newfile.createNewFile();
catch (IOException e)


imgUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK)

Uri imageUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newfile);

Bitmap bitmap = null;
try
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
catch (Exception e)
e.printStackTrace();


bitmap = Bitmap.createBitmap(bitmap,0,((bitmap.getHeight()-bitmap.getWidth()))/2,bitmap.getWidth(),bitmap.getWidth());
bitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, true);
img.setImageBitmap(bitmap);





I am getting the error Could not write image : java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
And as a result of this the bitmap cannot be loaded and my bitmap is null, so that i receive a NullpointerException










share|improve this question
























  • Does newfile.createNewFile() succeed?

    – Alex Cohn
    Nov 13 '18 at 19:35















0















I am trying to take a picture in Android using an Uri. But I am struggeling getting the bitmap from the created Uri. I always get a null-Object from my Uri.



Uri imgUri;
File newfile;
public static Button camButton;
public static ImageView img;
public static TextView text;

public void photo(Button cB, ImageView im, TextView tv)

text = tv;
camButton = cB;
img = im;

camButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
String file = dir + "test" + ".png";
newfile = new File(file);
try
newfile.createNewFile();
catch (IOException e)


imgUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK)

Uri imageUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newfile);

Bitmap bitmap = null;
try
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
catch (Exception e)
e.printStackTrace();


bitmap = Bitmap.createBitmap(bitmap,0,((bitmap.getHeight()-bitmap.getWidth()))/2,bitmap.getWidth(),bitmap.getWidth());
bitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, true);
img.setImageBitmap(bitmap);





I am getting the error Could not write image : java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
And as a result of this the bitmap cannot be loaded and my bitmap is null, so that i receive a NullpointerException










share|improve this question
























  • Does newfile.createNewFile() succeed?

    – Alex Cohn
    Nov 13 '18 at 19:35













0












0








0








I am trying to take a picture in Android using an Uri. But I am struggeling getting the bitmap from the created Uri. I always get a null-Object from my Uri.



Uri imgUri;
File newfile;
public static Button camButton;
public static ImageView img;
public static TextView text;

public void photo(Button cB, ImageView im, TextView tv)

text = tv;
camButton = cB;
img = im;

camButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
String file = dir + "test" + ".png";
newfile = new File(file);
try
newfile.createNewFile();
catch (IOException e)


imgUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK)

Uri imageUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newfile);

Bitmap bitmap = null;
try
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
catch (Exception e)
e.printStackTrace();


bitmap = Bitmap.createBitmap(bitmap,0,((bitmap.getHeight()-bitmap.getWidth()))/2,bitmap.getWidth(),bitmap.getWidth());
bitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, true);
img.setImageBitmap(bitmap);





I am getting the error Could not write image : java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
And as a result of this the bitmap cannot be loaded and my bitmap is null, so that i receive a NullpointerException










share|improve this question
















I am trying to take a picture in Android using an Uri. But I am struggeling getting the bitmap from the created Uri. I always get a null-Object from my Uri.



Uri imgUri;
File newfile;
public static Button camButton;
public static ImageView img;
public static TextView text;

public void photo(Button cB, ImageView im, TextView tv)

text = tv;
camButton = cB;
img = im;

camButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)

final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
String file = dir + "test" + ".png";
newfile = new File(file);
try
newfile.createNewFile();
catch (IOException e)


imgUri = FileProvider.getUriForFile(MainActivity.this,
BuildConfig.APPLICATION_ID + ".provider",
newfile);

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

);


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK)

Uri imageUri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", newfile);

Bitmap bitmap = null;
try
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
catch (Exception e)
e.printStackTrace();


bitmap = Bitmap.createBitmap(bitmap,0,((bitmap.getHeight()-bitmap.getWidth()))/2,bitmap.getWidth(),bitmap.getWidth());
bitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, true);
img.setImageBitmap(bitmap);





I am getting the error Could not write image : java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
And as a result of this the bitmap cannot be loaded and my bitmap is null, so that i receive a NullpointerException







android android-camera-intent android-fileprovider






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 19:26









Alex Cohn

41.2k552188




41.2k552188










asked Nov 13 '18 at 19:11









AlexAlex

245




245












  • Does newfile.createNewFile() succeed?

    – Alex Cohn
    Nov 13 '18 at 19:35

















  • Does newfile.createNewFile() succeed?

    – Alex Cohn
    Nov 13 '18 at 19:35
















Does newfile.createNewFile() succeed?

– Alex Cohn
Nov 13 '18 at 19:35





Does newfile.createNewFile() succeed?

– Alex Cohn
Nov 13 '18 at 19:35












1 Answer
1






active

oldest

votes


















-1














try with this: 
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)

Uri selectedImage = data.getData();
Bitmap photo = null;
try
photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
catch (IOException e)
e.printStackTrace();


ByteArrayOutputStream baos = new ByteArrayOutputStream();

photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

byte b = baos.toByteArray();

String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
byte decodedString = Base64.decode(encoded, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
CircularImageView imageView = findViewById(R.id.profileimage);
imageView.setImageBitmap(decodedByte);









share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287982%2fandroid-take-picture-with-uri%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    try with this: 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)

    Uri selectedImage = data.getData();
    Bitmap photo = null;
    try
    photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
    catch (IOException e)
    e.printStackTrace();


    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    byte b = baos.toByteArray();

    String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
    byte decodedString = Base64.decode(encoded, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    CircularImageView imageView = findViewById(R.id.profileimage);
    imageView.setImageBitmap(decodedByte);









    share|improve this answer



























      -1














      try with this: 
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data)
      super.onActivityResult(requestCode, resultCode, data);

      if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)

      Uri selectedImage = data.getData();
      Bitmap photo = null;
      try
      photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
      catch (IOException e)
      e.printStackTrace();


      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

      byte b = baos.toByteArray();

      String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
      byte decodedString = Base64.decode(encoded, Base64.DEFAULT);
      Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
      CircularImageView imageView = findViewById(R.id.profileimage);
      imageView.setImageBitmap(decodedByte);









      share|improve this answer

























        -1












        -1








        -1







        try with this: 
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)

        Uri selectedImage = data.getData();
        Bitmap photo = null;
        try
        photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
        catch (IOException e)
        e.printStackTrace();


        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        byte b = baos.toByteArray();

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
        byte decodedString = Base64.decode(encoded, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        CircularImageView imageView = findViewById(R.id.profileimage);
        imageView.setImageBitmap(decodedByte);









        share|improve this answer













        try with this: 
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)

        Uri selectedImage = data.getData();
        Bitmap photo = null;
        try
        photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
        catch (IOException e)
        e.printStackTrace();


        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        byte b = baos.toByteArray();

        String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
        byte decodedString = Base64.decode(encoded, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        CircularImageView imageView = findViewById(R.id.profileimage);
        imageView.setImageBitmap(decodedByte);










        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 21:21









        Vishal SharmaVishal Sharma

        1




        1



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287982%2fandroid-take-picture-with-uri%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3