1

what is the reason of this error :

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.sqlfirst.AddImage}: java.lang.NullPointerException: uriString

the error is pointing at this line where I am receiving the intent

img.setImageURI(Uri.parse(imagePath ));

I am trying to send a sd card path through an intent to another acitivity and convert it into an image this is the code:

Here sending the path of the image in sd card

public   void openGallery() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

        // startActivityForResult(
          // Intent.createChooser(intent, "Complete action using"),2);
       }

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {

           super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

   Bundle extras2 = data.getExtras();

                filePath = data.getData();
  Intent i = new Intent(this,
                        AddImage.class);
  i.putExtra("imagepath", filePath);
 startActivity(i);

here I suppose to recieve the path of the image and decrease the size of the image.

    String imagePath = getIntent().getStringExtra("imagePath");
    ImageView img=(ImageView) findViewById(R.id.imageView);
    img.setImageURI(Uri.parse(imagePath ));

    Bitmap bitmap = ((BitmapDrawable)img.getDrawable()).getBitmap();
    Bitmap out = Bitmap.createScaledBitmap(bitmap, 500, 500, false); 
    // bitmap is the image 
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
   out.compress(Bitmap.CompressFormat.JPEG, 60, stream); 
   bitmap.recycle();
Moudiz
  • 7,211
  • 22
  • 78
  • 156

2 Answers2

2

To use the path correctly you should first create it to have somewhere to store it, you can use that and later on delete it or use the data from the result you got.

Here is the code to create a Uri, pass it to your Intent. Once you get the result you can pass the Uri to another class by using getPath from the Uri package.

/**
         * Creating file uri to store image/video
         */
        public static Uri getOutputMediaFileUri() {
            return Uri.fromFile(getOutputMediaFile());
        }

    /**
     * returning image / video
     */
    private static File getOutputMediaFile() {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "YOUR DIRECTORY NAME");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("image upload", "Oops! Failed create "
                        + "YOUR DIRECTORY NAME" + " directory");
                return null;
            }
        }

        //TODO change naming
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
        return mediaFile;
    }

EDIT 1:

To convert a file to a bitmap you can use this code, provided by @Nikhilreddy Gujjula in this question

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
selected_photo.setImageBitmap(bitmap);
Community
  • 1
  • 1
Angmar
  • 599
  • 4
  • 9
  • Excellent ..is it possible to explain for me how to get uri from another activity and transform it into image ? Ur help ia really apreciate – Moudiz Sep 15 '15 at 16:32
  • weird but I am having BackendConsts cannot be resolved to a variable – Moudiz Sep 16 '15 at 11:54
  • @Moudiz sorry, edited the answer, that is where you put your directory name to put your file – Angmar Sep 16 '15 at 13:34
  • thank you , side question what is the different between onactivityresul , and your class ? if i can do it with your way , why using onactivityresult ? – Moudiz Sep 16 '15 at 14:06
  • You still need to use onactivityresult, but you need to create a file in order to have somewhere to place your file: be it an image, video or whatever you need – Angmar Sep 16 '15 at 14:42
  • sorry its really stupid answer but where should I call your method in my open fallery method ? but its intent ? – Moudiz Sep 16 '15 at 15:40
  • Before your intent, and send your file path to the intent so the intent has somewhere to put the file you are asking for. – Angmar Sep 16 '15 at 16:12
  • honestly it didnt work with me I dunno why , I create the 2 methoed before open gallery , then in open gallery I called them but nothing happend . anyway I apreciat your help , though I still couldnt fix this and I have to rely on google – Moudiz Sep 17 '15 at 20:55
1

You are setting imagepath as an extra and trying to retrieve imagePath.

Also note that setImageURI() is not a good choice, as it will load and decode the image on the main application thread. Use an image-loading library, like Picasso or Universal Image Loader.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • `setimageuri()` was suggested from a [`user`](http://stackoverflow.com/questions/32584648/how-to-send-from-onactivityresult-uri-path-to-another-activity-and-change-it-to/32585004?noredirect=1#comment53029081_32585004) .. i am already using picasso but I am having problem with decreasing the size of the image. is there an answer in SO where they explain how to get the path of image from SD card and send it to another activity then decrease its size ? – Moudiz Sep 15 '15 at 15:49
  • @Moudiz you can decrease the size of an image inside a Picasso holder by calling its fit() method when loading the image. – Angmar Sep 15 '15 at 15:52
  • @Angmar i did that and it didnt work the size still 200kb , I want to make it 50kb or less without loosing alot of its quality – Moudiz Sep 15 '15 at 15:54
  • Then try Universal File Loader, I think it actually shrinks the image to the size needed...or you can do it by yourself, here is a tutorial http://stackoverflow.com/questions/23740307/load-large-images-with-picasso-and-custom-transform-object?answertab=votes#tab-top – Angmar Sep 15 '15 at 15:58
  • @Angmar this is my question related to picasso http://stackoverflow.com/questions/32551633/how-to-resize-an-image-without-being-blurred-or-with-using-picasso and ill go through your comment , but can you please help me how to send a path image to another intent and transfer it into bitmap ? a SO link would be helpful – Moudiz Sep 15 '15 at 16:00
  • @Moudiz by the way, resizing an image without loosing quality using the same image type is not possible...when you resize you lower the amount of pixels in the image ergo lowering its quality. You can use the different Picasso methods (centerCrop, centerInside) or ImageView attributes (centerCrop, fitXY, etc.) to play with the image visualization. – Angmar Sep 15 '15 at 16:03
  • @Angmar ill check your link about that .. meanwhile do you have a SO link about how to send a path image to another intent and transfer it into bitmap ? – Moudiz Sep 15 '15 at 16:10
  • @Moudiz I'll post it as an answer – Angmar Sep 15 '15 at 16:17