【问题标题】:Store the taken picture from ImageView to the phone storage将ImageView中拍摄的图片存储到手机存储中
【发布时间】:2015-04-26 16:16:25
【问题描述】:

拍完照片后,我将图片存储到 ImageView 中,因此,如果有人对如何在 ImageView 上显示的图片在无需用户交互的情况下将其存储到手机阶段有任何想法或建议 在此先感谢

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);
    }

}

}

【问题讨论】:

标签: java android


【解决方案1】:

试试:

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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多