0

Here is the code. I tried to pass the image path from my Main_Activity to this new Activity, but the image is not being displayed in New_Activity.

So my question is: How is it possible to display the image in New_Activity by passing the path from the Main_Activity?

Is there any easier method to do such a thing?

Please explain it with details if possible. Example codes are welcome too... Thanks

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity2);
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            String filename = extras.getString("map1");
            FileInputStream is = null;
            BufferedInputStream bis = null;
            try {
                is = new FileInputStream(new File(filename));
                bis = new BufferedInputStream(is);
                Bitmap bitmap = BitmapFactory.decodeStream(bis);
                myImage = (ImageView)findViewById(R.id.imageView2);
                myImage.setImageBitmap(bitmap);    
                } 
            catch (Exception e) {
                //Try to recover
            }
            finally {
                try {
                    if (bis != null) {
                        bis.close();
                    }
                    if (is != null) {
                        is.close();
                    }    
                } catch (Exception e) {
                }
            }  

            myImage.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
}   

here is the logcat file:

07-30 20:36:46.601: W/MapDbAdapter(387): 5
07-30 20:37:33.161: D/AndroidRuntime(387): Shutting down VM
07-30 20:37:33.161: W/dalvikvm(387): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-30 20:37:33.172: E/AndroidRuntime(387): FATAL EXCEPTION: main
07-30 20:37:33.172: E/AndroidRuntime(387): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity.map/com.example.activity.map.Activity2}: java.lang.NullPointerException
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.os.Looper.loop(Looper.java:123)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 20:37:33.172: E/AndroidRuntime(387):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 20:37:33.172: E/AndroidRuntime(387):  at java.lang.reflect.Method.invoke(Method.java:521)
07-30 20:37:33.172: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 20:37:33.172: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 20:37:33.172: E/AndroidRuntime(387):  at dalvik.system.NativeStart.main(Native Method)
07-30 20:37:33.172: E/AndroidRuntime(387): Caused by: java.lang.NullPointerException
07-30 20:37:33.172: E/AndroidRuntime(387):  at com.example.activity.map.Activity2.onCreate(Activity2.java:48)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 20:37:33.172: E/AndroidRuntime(387):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-30 20:37:33.172: E/AndroidRuntime(387):  ... 11 more

THis is the new logcat file:

07-30 21:12:25.921: W/MapDbAdapter(493): 5 07-30 21:12:28.621: D/AndroidRuntime(493): Shutting down VM 07-30 21:12:28.621: W/dalvikvm(493): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 07-30 21:12:28.631: E/AndroidRuntime(493): FATAL EXCEPTION: main 07-30 21:12:28.631: E/AndroidRuntime(493): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity.map/com.example.activity.map.Activity2}: java.lang.NullPointerException 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.os.Handler.dispatchMessage(Handler.java:99) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.os.Looper.loop(Looper.java:123) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.ActivityThread.main(ActivityThread.java:4627) 07-30 21:12:28.631: E/AndroidRuntime(493): at java.lang.reflect.Method.invokeNative(Native Method) 07-30 21:12:28.631: E/AndroidRuntime(493): at java.lang.reflect.Method.invoke(Method.java:521) 07-30 21:12:28.631: E/AndroidRuntime(493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-30 21:12:28.631: E/AndroidRuntime(493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-30 21:12:28.631: E/AndroidRuntime(493): at dalvik.system.NativeStart.main(Native Method) 07-30 21:12:28.631: E/AndroidRuntime(493): Caused by: java.lang.NullPointerException 07-30 21:12:28.631: E/AndroidRuntime(493): at com.example.activity.map.Activity2.onCreate(Activity2.java:29) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 07-30 21:12:28.631: E/AndroidRuntime(493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 07-30 21:12:28.631: E/AndroidRuntime(493): ... 11 more

The error was in line number 29: i got it : here is also my java code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);
    String filename = getIntent().getStringExtra("map1");
    Bitmap bitmap = BitmapFactory.decodeFile(filename);

    if (bitmap != null) {
        myImage = (ImageView)findViewById(R.id.imageView2);
        myImage.setImageBitmap(bitmap);    
        } 



            myImage.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
//      private Bitmap getBitmapFromAsset(String strName){
//            AssetManager assetManager = getAssets();
//            InputStream istr = null;
//            try {
//                istr = assetManager.open(strName);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            Bitmap bitmap = BitmapFactory.decodeStream(istr);
//            return bitmap;
//        }
}   
Niranjan
  • 19
  • 11
  • are you getting a error? – Dheeraj Bhaskar Jul 30 '14 at 20:17
  • ya it says null pointer exception! but i dont know what is the problem !do you have any idea how does it really works or any other tutorial to get to know more about passing image path and getting to display the image in newactivity ! thanks in advance – Niranjan Jul 30 '14 at 20:17
  • Is your image in Drawable folder? – Ovi Jul 30 '14 at 20:28
  • please paste the stacktrace, that's the first thing you should post when you have an error – Dheeraj Bhaskar Jul 30 '14 at 20:32
  • No my image is in assets folder: for that i have to pass the path of the image in the assets folder. – Niranjan Jul 30 '14 at 20:33
  • This will answer your question: [Load an image from assets folder](http://stackoverflow.com/questions/11734803/load-an-image-from-assets-folder) – Ovi Jul 30 '14 at 20:44
  • hey Dheeraj, there is no stacktrace for the first Main_Activity part ! the application runs in the emulator but when i click and go to the New_Activity application crashes and i get the above log cat message as error ! – Niranjan Jul 30 '14 at 20:47
  • Which line 48 in Activity2? Plus you can't access assets folder like that. See my answer – Dheeraj Bhaskar Jul 30 '14 at 20:49

4 Answers4

0

This is how you show image from Assets folder

What you're doing is incorrect. Use this code and let usknow.

You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap.

private Bitmap getBitmapFromAsset(String strName)
    {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }
Community
  • 1
  • 1
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
  • 07-30 20:46:08.961: W/MapDbAdapter(411): 5 07-30 20:46:39.331: D/AndroidRuntime(411): Shutting down VM 07-30 20:46:39.331: W/dalvikvm(411): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 07-30 20:46:39.341: E/AndroidRuntime(411): FATAL EXCEPTION: main 07-30 20:46:39.341: E/AndroidRuntime(411): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activity.map/com.example.activity.map.Activity2}: java.lang.NullPointerException 07-30 20:46:39.341: E/AndroidRuntime(411): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) – Niranjan Jul 30 '14 at 21:01
  • On which line is the NPE? Please update your question with these stacktraces as formatting is lost in comments and it's difficult to read. Just leave me a comment when you update your question with what I've asked. – Dheeraj Bhaskar Jul 30 '14 at 21:04
  • hey Dheeraj ! i have updated the code ! the error was in line 29 ! now i got it ! – Niranjan Jul 30 '14 at 21:19
  • @Niranjan what was the mistake in your code, what did you figure out? – Dheeraj Bhaskar Jul 31 '14 at 22:33
0

Set the path for Image and Save your image to SD card after that Send your image path(image path you get as a string) to next Activity and get Image Again with image path.

Save Image to Sd Card and create an image Path for your Bitmap

private void saveImage() {
    File rootsd = Environment.getExternalStorageDirectory();
    File mediaStorageDir = new File(rootsd.getAbsolutePath()
            + "/YOUR_DIR/");

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("log_check", "failed to create directory");
            return;
        }
    }
    String fileName= "your_file_name"

    savedFileName = new File(mediaStorageDir.getAbsolutePath(), fileName
            + ".jpg");

    try {
        FileOutputStream fos = new FileOutputStream(savedFileName);

        yourBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("FileNotFoundException", e + "");
    } catch (IOException e) {
        Log.e("IOException", e + "");
    }

}

Send your "fileName" to next Activity.

On Next Activty You can create again a BitMap with your file name like below code.

String imagePath = getIntent().getExtras().getString("imagePath");
Bitmap yourBitmap = BitmapFactory.decodeFile(imagePath);
Raja
  • 36
  • 3
0
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String filename = getIntent().getStringExtra("map1");
    Bitmap bitmap = BitmapFactory.decodeFile(filename);

    if (bitmap != null) {
        myImage = (ImageView)findViewById(R.id.imageView2);
        myImage.setImageBitmap(bitmap);    
        } 
Daniel H
  • 389
  • 4
  • 19
0

This is the answer of the question. and it works perfectly !But i am passing image instead of passing the path of image and it is much easier !

public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity2);

            ImageView gallerypic = (ImageView) findViewById(R.id.imageView2);
/*Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.
*/
            String backgroundpath = getIntent().getExtras().getString("maps");


            if(getImageFromAssert(backgroundpath)!=null){
/*Sets a drawable as the content of this ImageView.*/
                gallerypic.setImageDrawable(getImageFromAssert(backgroundpath));
            }
        }

        private Drawable getImageFromAssert(String name){
            try {
/*read the image file from the assets */
                InputStream ims = getAssets().open(name);
/*create a drawable from the inputstream and return it */
                return Drawable.createFromStream(ims, null);
            }catch (Throwable e){
                e.printStackTrace();
            }
            return null;
        }
Niranjan
  • 19
  • 11