简体   繁体   中英

How can I save images to the app instead of the gallery?

I'm making an app that will let users password-protect photos. So far, the user can take a photo and the photo gets displayed. However, all the photos get saved to camera roll. How can I save them to the app instead of camera roll so they can be private, but still be able to access them from their uri (which I will save to SharedPreferences) ?

public class MainActivity extends AppCompatActivity {

    ListView listView;
    int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
    Uri imageUri;

    ArrayAdapter<String> adapter;
    ArrayList<String> arrayList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
            }
        });
    }


    public void takePic(View view) {

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageUri = Uri.fromFile(new File(getApplicationContext().getFilesDir(), String.valueOf(System.currentTimeMillis()) ));
        intent.putExtra("data", imageUri);
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
}





}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

            //use imageUri here to access the image

            Bundle extras = data.getExtras();

            Log.e("URI", imageUri.toString());
            Bitmap bmp = (Bitmap) extras.get("data");
            ImageView imageView = (ImageView) findViewById(R.id.imageView);

            imageView.setImageBitmap(bmp);

            // here you will get the image as bitmap


        }
        else if (resultCode == RESULT_CANCELED) {
            Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
        }
    }


}

You will need to store the image file in the internal directory, that does not let the gallery app access it.

Instead of Environment.getExternalStorageDirectory() , use getApplicationContext().getFilesDir();

If you care a lot of security, I think you should go with using getFilesDir() . A minor note, I faced an issue using this approach. I thought it is something you should be aware of.

You could also use getApplicationContext.getExternalFilesDir(null); ; I passed null since I didn't want to put Environment.DIRECTORY_PICTURES, which would allow the Media Scanner to find the image file and put it in gallery. Here are the docs for my suggestions getExternalFileDir() , getFilesDir() .

Let me know how it goes :)

EDIT:

Regardless, since the Media Scanner is pretty smart, if the above approaches still didn't work....

An addition to my answer, if the Media Scanner can still find the photo and put in in the gallery, there is a small trick you can play to fool it. Change the file extension for a password-protected photo. Only your app will then know the secret extension. This may be achieved by hashing the filename or simply adding something like "hahaFooledYou" to the filename, or getting rid of the extension. This, although not the slickest way, will work. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM