如何在应用程序的内部存储器中放置图片? [英] how to put a picture in the internal memory of the app?

查看:71
本文介绍了如何在应用程序的内部存储器中放置图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从相机应用程序拍摄一张照片并将其保存到该应用程序的文件中,直到现在搜索使我对Uris内容有所了解.我已经能够将照片保存在外部存储器中,因此,如果可以将照片从那里移动到apk文件中,那就太好了.

I am trying to take a picture from the camera app and save it into the app's files , searching until now lead me to something about content Uris.. (I have no idea on what they are or how to use them). I am already able to save a photo in the external memory so if there is a way to move photos from there into the apk file it would be nice.

我要放入照片的路径是"appname/app/src/main/assets/photos"

the path I wanted to put the photos in is "appname/app/src/main/assets/photos"

这是我现在拥有的代码:

this is the code I have now:

public class Add_Comment_Picture extends AppCompatActivity {
    static final int CAM_REQUEST = 1;
    ImageView imageView;
    ImageButton button;
    private static final int REQUEST_CODE = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__comment__picture);
    //    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
        button = (ImageButton) findViewById(R.id.imageButtonCamera);
        imageView = (ImageView) findViewById(R.id.imageView);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent , CAM_REQUEST);
            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);

            List<String> permissions = new ArrayList<String>();
            if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            }

            if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.CAMERA);
            }
            if (!permissions.isEmpty()) {
                requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
            }
        }


    }

    private File getFile()
    {
        File folder = new File("sdcard/camera_app");
        if (!folder.exists())
        {
            folder.mkdir();
        }
        File image_file = new File(folder,"cam_image.jpg");
        return image_file;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/camera_app/cam_image.jpg";
        imageView.setImageDrawable(Drawable.createFromPath(path));
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 111: {
                for (int i = 0; i < permissions.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);


                    } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);

                    }
                }
            }
            break;
            default: {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }
}

推荐答案

您的apk文件有点像android应用程序安装文件.这是一个压缩文件,您也可以使用7-Zip或类似程序打开它.在设备上安装应用程序后,文件将被提取到内存中.

Your apk file is somthing like an android app installation file. It is a zipped file, you can also open it with 7-Zip or a similar program. When you install an app on your device the file will be extracted to your internal memory.

URI代表统一资源标识符.它可以识别您的资源,以您所拍的照片为准.

URI stands for Uniform Resource Identifier. It identifies your resource, in your case the taken picture.

您可以将图片保存到内部或外部存储器中.如果要使用外部存储器,则需要权限:

You can save the picture to your internal or external memory. If you want to use the external memory you need permissions:

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

如果将文件保存到内存中,则其他应用程序将看不到该文件,并且无法使用af文件浏览器找到它.如果它是外部的,则可以在文件浏览器中找到它.

If you save the file to your internal memory its is invisible to other apps and you cannot find it with af file explorer. If it is external you will find it in your file explorer.

在Stackoverflow上查看有关如何保存相机文件的以下答案:

Check this answer on Stackoverflow on how to save your camera file:

Stackoverflow摄像头意图

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

然后,在onActivityResult中,将 data.getData()替换为 fileUri.toString(),它将解决您的问题.

Then, in onActivityResult, replace data.getData() with fileUri.toString() and it will solve your problem.

这篇关于如何在应用程序的内部存储器中放置图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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