无法在画廊中显示图像

时间:2014-11-07 07:30:24

标签: android gallery

我想显示拍摄和保存的图像,但即使我在图库中选择各种图像,也只显示一个图像。 我在想,但我不知道是什么原因。 我想知道我是如何误用数据库的?或者如何使用文件? 请告诉我如何解决这个问题。

public class MyActivity extends Activity {
    ImageView imageView;
    SharedPreferences preferences;
    int imageNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        imageView=(ImageView)findViewById(R.id.picture);
        imageView.setImageBitmap(null);
        Button galleryButton=(Button)findViewById(R.id.gallery_button);
        galleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction(intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, 0);
            }
        });
        Button cameraButton=(Button)findViewById(R.id.camera_button);
        cameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setAction("android.media.action.IMAGE_CAPTURE");
                startActivityForResult(intent, 1);
            }
        });
        preferences=getSharedPreferences("pic_num",Activity.MODE_PRIVATE);
        imageNum=preferences.getInt("pic_num",0);
    }

    @Override
    protected void onActivityResult(int requestCord,int resultCode,Intent data){
        if(requestCord==0){
            if(resultCode==RESULT_OK){
                try{
                    InputStream is=getContentResolver().openInputStream(data.getData());
                    Bitmap pic=BitmapFactory.decodeStream(is);
                    savePic(pic);
                    Uri uri = Uri.parse("content://com.example.appdev.photoprovider/pic");
                    ContentValues values=new ContentValues();
                    values.put("pic_path","pic"+imageNum+".png");
                    getContentResolver().insert(uri,values);
                    is.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
                imageNum++;
                SharedPreferences.Editor editor=preferences.edit();
                editor.putInt("pic_num",imageNum);
                editor.commit();
            }
        }else if(requestCord==1){
            if(resultCode==RESULT_OK) {
                try {
                    Bitmap pic=(Bitmap)data.getExtras().get("data");
                    savePic(pic);
                    Uri uri = Uri.parse("content://com.example.appdev.photoprovider/pic");
                    ContentValues values = new ContentValues();
                    values.put("pic_path","pic"+imageNum+".png");
                    getContentResolver().insert(uri, values);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                imageNum++;
                SharedPreferences.Editor editor=preferences.edit();
                editor.putInt("pic_num",imageNum);
                editor.commit();
            }
        }
    }

    public void savePic(Bitmap bitmap){
        try{
            OutputStream out=openFileOutput("pic"+imageNum+".png", Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        final String[] paths = {getFilesDir()+"/pic"+imageNum+".png"};
        String[] mimeType = {"image/png"};
        MediaScannerConnection.scanFile(getApplicationContext(),paths,mimeType,new MediaScannerConnection.OnScanCompletedListener() {
            @Override
            public void onScanCompleted(String s, Uri uri) {
            }
        });
    }

    @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_update) {
            Uri uri=Uri.parse("content://com.example.appdev.photoprovider/pic");
            Cursor cursor=getContentResolver().query(uri,new String[]{"pic_path"},null,null,null);
            cursor.moveToLast();
            String picPath=cursor.getString(0);
            try{
                InputStream is=openFileInput(picPath);
                imageView.setImageBitmap(BitmapFactory.decodeStream(is));
            }catch (Exception e){
                e.printStackTrace();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定,但这行代码看起来像个问题:

 }else if(requestCord==1){

看起来应该是

 }else if(requestCode==1){

也许这会有所帮助......