为什么我在画廊的照片没有显示?

时间:2014-11-14 05:21:36

标签: android android-gallery

我想从我的图库中选择一张图片,并在ImageView中显示。 但图片没有显示。为什么? 这是生命周期的问题?或者如何使用setImageBitmap()是错误的? 我该怎么办? 请帮帮我。

- 这是全班 -

public class RegisterView extends Activity {
TextView textCategory;
ImageView imageButton;
EditText productName,price;
private final static int CATEGORY=0;
private final static int GALLERY=1;
private final static int CAMERA=2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register_view);
    textCategory=(TextView)findViewById(R.id.text_category);
    imageButton=(ImageView)findViewById(R.id.select_image);
    productName=(EditText)findViewById(R.id.product_name);
    price=(EditText)findViewById(R.id.price);
    Button categoryButton=(Button)findViewById(R.id.category_button);
    categoryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(RegisterView.this,CategoryList.class);
            startActivityForResult(intent,CATEGORY);
        }
    });
    ImageView selectImage=(ImageView)findViewById(R.id.select_image);
    selectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment dialog=new SelectImageDialog();
            dialog.show(getFragmentManager(),"dialog");
        }
    });
    Button saveButton=(Button)findViewById(R.id.save_button);
    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(textCategory.getText().toString().equals("") || imageButton.getDrawable()==null || productName.getText().toString().equals("") || price.getText().toString().equals("")) {
                Bundle bundle=new Bundle();
                bundle.putString("message","全ての項目を入力してください");
                DialogFragment dialog=new CategoryList.AttensionDialog();
                dialog.setArguments(bundle);
                dialog.show(getFragmentManager(),"dialog");
            }else{
                DialogFragment dialog = new SaveTimeDialog();
                dialog.show(getFragmentManager(), "dialog");
            }
        }
    });
}

public static class SelectImageDialog extends DialogFragment{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setMessage("画像の設定方法を選択してください").setPositiveButton("カメラ",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent=new Intent();
                intent.setAction("android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent,CAMERA);
            }
        }).setNegativeButton("ギャラリー",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent=new Intent();
                intent.setAction(intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent,GALLERY);
            }
        });
        return builder.create();
    }
}

public static class SaveTimeDialog extends DialogFragment{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        builder.setMessage("データを登録しますか?").setPositiveButton("はい",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //入力されたデータをデータベースに登録する
            }
        }).setNegativeButton("いいえ",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        return builder.create();
    }
}

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
    if(requestCode==CATEGORY){
        if(resultCode==RESULT_OK) {
            Bundle bundle = data.getExtras();
            textCategory.setText(bundle.getString("item"));
        }
    }else if(requestCode==GALLERY){
        if(resultCode==RESULT_OK){
            try{
                InputStream is=getContentResolver().openInputStream(data.getData());
                Bitmap bitmap=BitmapFactory.decodeStream(is);
                is.close();
                imageButton.setImageBitmap(bitmap);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }else if(requestCode==CAMERA){
        if(resultCode==RESULT_OK){
            Bitmap bitmap=(Bitmap)data.getExtras().get("data");
            imageButton.setImageBitmap(savePic(bitmap));
        }
    }
}

public Bitmap savePic(Bitmap bitmap){
    String path= Environment.getExternalStorageDirectory().getPath();
    try {
        FileOutputStream outputStream = new FileOutputStream(path+"pic.png");
        bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
        outputStream.close();
    }catch (Exception e){
        e.printStackTrace();
    }
    String[] paths = {Environment.getExternalStorageDirectory().getPath()+"/pic.png"};
    String[] mimeType = {"image/png"};
    MediaScannerConnection.scanFile(getApplicationContext(), paths, mimeType, new MediaScannerConnection.OnScanCompletedListener() {
        @Override
        public void onScanCompleted(String s, Uri uri) {
        }
    });
    return bitmap;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是logcat的消息。

11-14 15:31:23.126  18201-18201/com.example.appdev.shop D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
11-14 15:31:23.126  18201-18201/com.example.appdev.shop D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
11-14 15:31:23.126  18201-18201/com.example.appdev.shop D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
11-14 15:31:23.126  18201-18201/com.example.appdev.shop I/Adreno200-EGL﹕ <qeglDrvAPI_eglInitialize:265>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.5.6.04.02.02.093.081_msm8960_JB_2.5.6_CL3544079_release_AU (CL3544079)
    Build Date: 06/27/13 Thu
    Local Branch:
    Remote Branch: m/jb_2.5.6
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_JB_2.5.6.04.02.02.093.081 +  NOTHING
11-14 15:31:23.156  18201-18201/com.example.appdev.shop D/OpenGLRenderer﹕ Enabling debug mode 0
11-14 15:31:27.301  18201-18201/com.example.appdev.shop W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection

1 个答案:

答案 0 :(得分:0)

关于KITKAT的问题。你需要做这个工作。

  public static final String KITKAT_VALUE = 1002;

  Intent intent;

  if (Build.VERSION.SDK_INT < 19){
       intent = new Intent();
       intent.setAction(Intent.ACTION_GET_CONTENT);
       intent.setType("*/*");
       startActivityForResult(intent, KITKAT_VALUE);
  } 
  else 
  {
       intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
       intent.addCategory(Intent.CATEGORY_OPENABLE);
       intent.setType("*/*");
       startActivityForResult(intent, KITKAT_VALUE);
  }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (resultCode == Activity.RESULT_OK && data!=null) {
             Uri selectedImage = data.getData();
             String[] filePathColumn = { MediaStore.Images.Media.DATA };

             Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
             cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            imageButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));

     }
}

参考:Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT

相关问题