将拍摄的照片从ImageView存储到手机存储空间

时间:2015-04-26 08:06:55

标签: java android

拍摄照片后,我将照片存储到ImageView中,所以如果有人有想法或建议如何将图片显示在ImageView上,而不需要用户互动 提前致谢

enter image description here

public class MainActivity extends Activity {
    ImageView viewpict;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpict=(ImageView) findViewById(R.id.pict_result);
        Button btn= (Button)findViewById(R.id.camera);

        btn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            //  Intent intent = new Intent (getApplicationContext(),MainActivity2.class);

                //startActivity(intent);
                startActivityForResult(intent,0);

            }

     });


    }

protected void onActivityResult( int requestCode, int resultCode,Intent data)
{
    if (requestCode==0)
    {
        Bitmap theimage = (Bitmap) data.getExtras().get("data");
        viewpict.setImageBitmap(theimage);
    }

}

}

1 个答案:

答案 0 :(得分:2)

尝试:

viewpict.buildDrawingCache();
Bitmap bm=viewpict.getDrawingCache();

并保存:

 OutputStream fOut = null;
    Uri outputFileUri;
     try {
    File root = new File(Environment.getExternalStorageDirectory()
      + File.separator + "folder_name" + File.separator);
    root.mkdirs();
   File sdImageMainDirectory = new File(root, "myPicName.jpg");
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
    fOut = new FileOutputStream(sdImageMainDirectory);
   } catch (Exception e) {
    Toast.makeText(this, "Error occured. Please try again later.",
      Toast.LENGTH_SHORT).show();
   }

   try {
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
   } catch (Exception e) {
   }

AndroidManifest中的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />