Where do I find the Saved Image in Android?

2019-03-01 04:02发布

问题:

Excuse me, quick question:

I have this routine of video stream, where I receive packets, convert them to byte[] ,then to bitmaps, then display them on the screen:

dsocket.receive(packetReceived); // receive packet
byte[] buff = packetReceived.getData(); // convert packet to byte[]
final Bitmap ReceivedImage = BitmapFactory.decodeByteArray(buff, 0, buff.length); // convert byte[] to bitmap image

runOnUiThread(new Runnable()
{
    @Override
    public void run()
    {
        // this is executed on the main (UI) thread
        imageView.setImageBitmap(ReceivedImage);
    }
});

Now, I want to implementing a Recording feature. Suggestions say I need to use FFmpeg (which I have no idea how) but first, I need to prepare a directory of the ordered images to then convert it to a video file. Do do that I will save all the images internally, and I am using this answer to save each image:

if(RecordVideo && !PauseRecording) {
    saveToInternalStorage(ReceivedImage, ImageNumber);
    ImageNumber++;
}
else
{
    if(!RecordVideo)
        ImageNumber = 0;
}

// ...

private void saveToInternalStorage(Bitmap bitmapImage, int counter){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());

        // path to /data/data/yourapp/app_data/imageDir
        File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);

        // Create imageDir
        File MyPath = new File(MyDirectory,"Image" + counter + ".jpg");

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(MyPath);

            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //return MyDirectory.getAbsolutePath();
    }

But I can't seem to find the directory on my device (To actually see if I succeeded in creating the directory) Where is // path to /data/data/yourapp/app_data/imageDir located exactly?

回答1:

The getDir method of ContextWrapper will automatically create the imageDir directory if it does not already exist according to the docs. Additionally, you cannot access anything in the /data directory outside of your application code unless you have root access. If you would like to view the images you saved in this directory, you can run the adb tool in a command prompt to move the images into a publicly accessible directory:

adb shell run-as com.your.packagename cp -r /data/data/com.your.packagename/app_data/imageDir /sdcard/imageDir

Note that the run-as command will only work if your application is debuggable.

You can replace /sdcard/imageDir with any directory you have permission to access on the device. If you would like to subsequently move the files off the device and onto your machine, you can use adb pull to pull the files from the public directory:

adb pull /sdcard/myDir C:\Users\Desktop

Again, replace /sdcard/myDir and C:\Users\Desktop with the appropriate source and destination directories.