从浏览器浏览图像时出现运行时错误(SD卡)

时间:2013-08-26 18:17:26

标签: android runtime-error android-gallery

现在我已将文件推送到设备上了 当i Log.i(“imagePath”,imagePath)时,方法findPath becuz没有错误;当我选择那个图像并且我认为它是关于(text.setText(msg);和image.setImageBitmap(imageData);和当我删除text.setText(msg)和image.setImageBitmap(imageData)时,它返回路径图像;这不是错误。请检查一下&&检查我的布局

这是我在mainActivity中的代码

package com.example.gallerymagazine;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
private static final int browse_image = 1;
private TextView text;
private Button browse;
private ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView text = (TextView) findViewById(R.id.text);
    ImageView image = (ImageView) findViewById(R.id.image);
    Button browse = (Button) findViewById(R.id.browse_image_button);

    browse.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "select app to  pick image"),browse_image);
        }
    });


  }
protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent){
    super.onActivityResult(requestCode, resultCode, returnedIntent);
    switch (requestCode){
    case browse_image:
        if(resultCode == RESULT_OK){
            // เมือคลิกที่รูป ข้อมูลจะถูกส่งผ่านพารามิเตอร์ returnedIntent โดยมีผลลัพธ์เป็น URI(ฝช้ในการหาไฟล์พาด)
            Uri imageUri = returnedIntent.getData();
            String msg = "URL::"+imageUri+"\n";
            // นำURI หาpath 
            String imagePath = findPath(imageUri);
            msg += "Path::"+imagePath;

            text.setText(msg);
            // นำpath ไปดึงรูปมาแสดง
            Bitmap imageData = BitmapFactory.decodeFile(imagePath);
            image.setImageBitmap(imageData);
        }
    }
}

// method find path
private String findPath(Uri imageUri) {
    // TODO Auto-generated method stub
    String imagePath;
    String[] columns = {MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(imageUri, columns, null, null, null);
    //จาก Gallery
    if(cursor != null){
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        imagePath = cursor.getString(columnIndex);

    }else{
        imagePath = imageUri.getPath();
    }
    return imagePath;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我选择图像时的运行时错误

08-26 17:58:17.423: E/AndroidRuntime(11560): FATAL EXCEPTION: main
08-26 17:58:17.423: E/AndroidRuntime(11560): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/12 }} to activity {com.example.gallerymagazine/com.example.gallerymagazine.MainActivity}: java.lang.NullPointerException
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.ActivityThread.access$1100(ActivityThread.java:141)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.os.Looper.loop(Looper.java:137)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.ActivityThread.main(ActivityThread.java:5041)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at dalvik.system.NativeStart.main(Native Method)
08-26 17:58:17.423: E/AndroidRuntime(11560): Caused by: java.lang.NullPointerException
08-26 17:58:17.423: E/AndroidRuntime(11560):    at com.example.gallerymagazine.MainActivity.onActivityResult(MainActivity.java:57)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.Activity.dispatchActivityResult(Activity.java:5293)
08-26 17:58:17.423: E/AndroidRuntime(11560):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
08-26 17:58:17.423: E/AndroidRuntime(11560):    ... 11 more
08-26 17:58:24.303: I/Process(11560): Sending signal. PID: 11560 SIG: 9

这是我的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
    android:id="@+id/browse_image_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Browse Image"/>
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:gravity="center"
    />
<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_gravity="center"
    android:layout_marginTop="16dp"
    android:scaleType="centerInside"
    />

请检查&amp;谢谢

2 个答案:

答案 0 :(得分:0)

试试这个:

        protected void onActivityResult(int requestCode, int resultCode, Intent returnedIntent){
            super.onActivityResult(requestCode, resultCode, returnedIntent);
            switch (requestCode){
            case browse_image:
        if(resultCode != RESULT_CANCELED){
                if(resultCode == RESULT_OK){
                    // เมือคลิกที่รูป ข้อมูลจะถูกส่งผ่านพารามิเตอร์ returnedIntent โดยมีผลลัพธ์เป็น URI(ฝช้ในการหาไฟล์พาด)
                    Uri imageUri = returnedIntent.getData();
                    String msg = "URL::"+imageUri+"\n";
                    // นำURI หาpath 
                    String imagePath = findPath(imageUri);
                    msg += "Path::"+imagePath;

                    text.setText(msg);
                    // นำpath ไปดึงรูปมาแสดง
                    Bitmap imageData = BitmapFactory.decodeFile(imagePath);
                    image.setImageBitmap(imageData);
                }
            } else {
    Log.e("Error", "error happened");
}
        }
        }

我将解释我做了什么,如果有效

<强>更新

更改Uri imageUri = returnedIntent.getData();

Uri imageUri = data.getData();

答案 1 :(得分:0)

在你的onCreate方法中改变它:

TextView text = (TextView) findViewById(R.id.text);
ImageView image = (ImageView) findViewById(R.id.image);
Button browse = (Button) findViewById(R.id.browse_image_button);

到此:

text = (TextView) findViewById(R.id.text);
image = (ImageView) findViewById(R.id.image);
browse = (Button) findViewById(R.id.browse_image_button);

原因是当你这样做时

TextView text = (TextView) findViewById(R.id.text);

您正在定义一个其他方法无法访问的局部变量。所以当你尝试引用它时,你会得到一个空指针异常。

通过 user2045570

回答