how to put a picture in the internal memory of the

2019-08-14 08:13发布

问题:

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.

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

回答1:

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

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.

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

Stackoverflow Camera Intent

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

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



回答2:

Well apk file means its bundle of your all class files, resources files(images,videos etc), validation certificate files and many more. As per your asset folder it will consider as resources files only the way it treated in programing is different. Once generated apk file in running environment its get converted dex files. You cannot modify the contents of the file.

As i said above saving at this path not possible. appname/app/src/main/assets/photos

But you can make you app internal memory, You can create files and use the when you want.



回答3:

You can not change assets value after your apk generation you can only do that store your photos in to data\data\yourpackagename\photos but this thing will increase you app size in device



回答4:

An apk file consists of :

assets, META-IFN, res foulder.

AndroidManifest.xml, classes.dex and resources.arsc

Once deployed on a device, apk generates a folder, typically in /data/data/your.package.name/ .

What i encourage you to do is save the files in the dedicated areas.



回答5:

There is no way to save photos which you are taking from the camera app, because those photos are created in runtime and files in your source folder ("appname/app/src/main/assets/photos") have to be added in compile time (even before compile time).