Android的 - 以照片,并通过意向使用自定义名称将其保存到一个自定义的目标 [英] Android - Taking photos and saving them with a custom name to a custom destination via Intent

查看:134
本文介绍了Android的 - 以照片,并通过意向使用自定义名称将其保存到一个自定义的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有意向通过打开相机拍照的程序。这多少部分工作正常了。不过,我希望它保存到​​特定的文件夹具有一定的文件名(文件名是可选的,但它真的会真正帮助)。

I have a program that opens the camera via Intent to take a photo. That much part works fine already. However, I want it to save to a certain folder with a certain file name (the file name is optional but it would really really help).

因此​​,这里是我到目前为止所。

So here's what I have so far.

下面是code线打开相机:

Here's the line of code that opens the camera:

//TODO camera stuff.
Intent openCam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

//The two lines of code below were commented out at first.
//They were eventually added when I tried to save it with a custom name and destination
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
openCam.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

startActivityForResult(openCam, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

结果处理放在这里:

Result handler goes here:

//TODO handle result
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
   if (resultCode == RESULT_OK) {
         // Image captured and saved to fileUri specified in the Intent
         Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
            System.out.println("I am here");

   } 
   else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
   } 
   else {
            // Image capture failed, advise user
   }
}

在我实现下面的两种方法中,code是工作的罚款。然而,它被保存到使用默认的文件名(时间戳版本)的默认文件夹。显示土司映像保存到:空。因为我还没有设置这部分尚未

Before I implemented the two methods below, the code was working fine. However it was saved to the default folder with the default file name (the time stamp version). The toast displayed "Imaged saved to: null" since I haven't set that part yet.

因此​​,这里有被认为处理自定义文件名和该方法的目标

So here are the methods that were supposed to handle the custom filename and destination

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    Environment.getExternalStorageState();

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

*这些code从相机指南摆脱了developer.android.com。

*These code were lifted from the Camera Guide from developer.android.com.

在code以上设法打开相机和拍照,并将它们保存。然而,当用户决定停止拍照和preSS的背面键出现问题。什么情况是,应用程序的力量关闭给这个错误:

The code above manages to open the Camera and take photos and save them. However, the problem occurs when the user decides to stop taking photos and press the back key. What happens is that the app force closes giving this error:

10-21 12:44:33.699: E/AndroidRuntime(13016): FATAL EXCEPTION: main
10-21 12:44:33.699: E/AndroidRuntime(13016): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.AIC.QRCodeScanner/com.AIC.QRCodeScanner.QRCodeScanner}: java.lang.NullPointerException
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.os.Looper.loop(Looper.java:123)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.main(ActivityThread.java:3691)
10-21 12:44:33.699: E/AndroidRuntime(13016): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 12:44:33.699: E/AndroidRuntime(13016): at java.lang.reflect.Method.invoke(Method.java:507)
10-21 12:44:33.699: E/AndroidRuntime(13016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
10-21 12:44:33.699: E/AndroidRuntime(13016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
10-21 12:44:33.699: E/AndroidRuntime(13016): at dalvik.system.NativeStart.main(Native Method)
10-21 12:44:33.699: E/AndroidRuntime(13016): Caused by: java.lang.NullPointerException
10-21 12:44:33.699: E/AndroidRuntime(13016): at com.AIC.QRCodeScanner.QRCodeScanner.onActivityResult(QRCodeScanner.java:379)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.Activity.dispatchActivityResult(Activity.java:3934)
10-21 12:44:33.699: E/AndroidRuntime(13016): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
10-21 12:44:33.699: E/AndroidRuntime(13016):    
... 11 more

行它指向将是此行(行379): data.getData(),Toast.LENGTH_LONG

不过,这些文件被保存在文件夹 /图片/ MyCameraApp 一起拍摄的Instagram的照片。

However, the files are saved at the folder /Pictures/MyCameraApp together with the Instagram photos that were taken.

所以,问题是: 1.有没有一种方法,使onActivityResult正常工作?我知道,我可以诉诸为了不杀应用程序使用startActivity。 2.有没有办法只能拍摄一张照片,用相机?所以用户保存后的照片,该应用返回到主活性。 3.另外,我可以将它保存到我自己的文件夹?我不知道为什么它保存照片 /图片/ MyCameraApp ,我希望它只保存到 / MyCameraApp

So the questions are: 1. Is there a way to make onActivityResult work properly? I know I can just resort to using startActivity in order not to kill the app. 2. Is there a way to only take one shot with the camera? So after the user saves the photo, the app goes back to the main activity. 3. Also, can I save it to a folder of my own? I'm not sure why it saves the photos in /Pictures/MyCameraApp, I want it to save only to /MyCameraApp.

我想我错过了简单的东西在这里。

I think I'm missing out on something simple here.

推荐答案

这里的code,使得它的工作:

Here's the code that made it work:

//camera stuff
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

//folder stuff
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();

File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
Uri uriSavedImage = Uri.fromFile(image);

imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

它打开相机和带一个镜头(它会返回给用户后的主要活动保存拍摄的图像,它保存图像到指定的文件夹中。

It opens the camera and takes exactly one shot (it goes back to the main activity after the user saves the image that was taken. It saves the image to the specified folder.

这篇关于Android的 - 以照片,并通过意向使用自定义名称将其保存到一个自定义的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆