【问题标题】:Saving bitmap at custom path and with custom name将位图保存在自定义路径和自定义名称
【发布时间】:2013-01-12 12:41:08
【问题描述】:

我的程序捕获图像(作为位图),但我不知道(也找不到)如何在自定义路径中使用自定义名称保存它...这是我的图像捕获代码。我可以做吗?或者有什么办法将它保存为另一种数据类型,而不是位图?一些帮助会很棒。谢谢。

public class ShowMessagesPage extends Activity {
final static int CAMERA_RESULT = 0;
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_messages_page);

    Button userButton = (Button)findViewById(R.id.button1);
    userButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        error();
        }});

    Button buttonPhoto = (Button)findViewById(R.id.buttonPhoto);
    buttonPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, CAMERA_RESULT);
        }});}

protected void onActivityResult(int requestCode, int resultCode,
          Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {
          Bundle extras = intent.getExtras();
          Bitmap bmp = (Bitmap) extras.get("data");



        }
      }

and the other codes..........

【问题讨论】:

标签: android bitmap save image-capture


【解决方案1】:

试试这个:

Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

File cameraFolder;

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"folder_name_of_your_choice");
else
    cameraFolder= ShowMessagesPage.this.getCacheDir();
if(!cameraFolder.exists())
    cameraFolder.mkdirs();

File photo = new File(Environment.getExternalStorageDirectory(), "folder_name_of_your_choice/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);

startActivityForResult(getCameraImage, CAMERA_RESULT);

在 onActivityResult() 中,处理Uri initialURI 以获取您的图像,该图像已保存到您在上面发布的代码中选择的路径中。图像文件的名称为:camera_snap.jpg

注意:Uri initialURI 是全局声明的。

【讨论】:

  • 对不起,但 Eclipse 说没有像“StatusUpdate”和“reqcdCameraImage”这样的方法或变量......也没有它们的导入:/
  • @ArdaOğulÜçpınar:我的错,我按原样粘贴了我的代码并忘记了这些修改。检查编辑。
  • OMG :D 我应该看到的 :D thanx :D
  • @ArdaOğulÜçpınar:发生在我们最好的人身上。 :-)
  • 我有一个问题...当我拍照并按“确定”时,程序不会返回到上一个活动。只是停留在相机活动中。 ://
猜你喜欢
  • 2021-12-09
  • 1970-01-01
  • 2014-01-26
  • 2013-12-05
  • 2014-07-13
  • 2018-04-04
  • 2015-08-21
  • 1970-01-01
  • 2011-05-07
相关资源
最近更新 更多