将图像URI从一个活动传递到其他活动,以在第二个活动的图像视图中显示来自图库的选定图像

时间:2016-01-20 12:30:38

标签: android image android-intent

我的MainActivity.java文件包含以下内容,我的应用程序在浏览分享后通过按钮从图库中的图像开始。

Intent intent = new Intent(MainActivity.this,BoardView.class);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("image", path);
startActivity(intent);

我名为BoardView的第二个活动接受图片为:

Intent ii = getIntent();
Bundle b = ii.getExtras();
if(b!= null) {
    String image = ii.getStringExtra("image");
    view_news.setVisibility(View.VISIBLE);
}

但我没有在第二次活动中获得我的形象。 实际上,第一个活动是登录,第二个活动在登录后显示图像。

编辑: Logcat显示错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chawlaz.cubetboard/com.example.chawlaz.cubetboard.BoardView}: java.lang.NullPointerException: uriString
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException: uriString
        at android.net.Uri$StringUri.<init>(Uri.java:467)
        at android.net.Uri$StringUri.<init>(Uri.java:457)
        at android.net.Uri.parse(Uri.java:429)
        at com.example.chawlaz.cubetboard.BoardView.onCreate(BoardView.java:156)
        at android.app.Activity.performCreate(Activity.java:5231)


    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                    at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                    at android.app.ActivityThread.access$800(ActivityThread.java:135)
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                    at android.os.Handler.dispatchMessage(Handler.java:102)
                    at android.os.Looper.loop(Looper.java:136)
                    

    at android.app.ActivityThread.main(ActivityThread.java:5001)
                        at java.lang.reflect.Method.invokeNative(Native Method)
                        at java.lang.reflect.Method.invoke(Method.java:515)
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

使用ImageView.setImageURI在ImageView中使用Uri显示图片:

String image = ii.getStringExtra("image");
view_news.setVisibility(View.VISIBLE);
Uri imgUri=Uri.parse(image);// << get URI from String
view_news.setImageURI(imgUri);

答案 1 :(得分:0)

声明全局静态变量

public static final String IMAGE_URL = "imageURL";

Intent intent = new Intent(MainActivity.this,BoardView.class);
intent.setType("image/*");
intent.putExtra(IMAGE_URL, path);
startActivity(intent);

在另一个班级

Intent ii = getIntent();
Bundle b = ii.getExtras();
if(b!= null) {
    String image = ii.getParcelableExtra(MainActivity.IMAGE_URL);
    view_news.setVisibility(View.VISIBLE);
}

希望这会帮助你