android trouble with saving pictures to internal memory

I'm writing an app for school, of which one of the biggest parts is taking pictures. I came to conclusion that it would be perfect if I could just save them on Internal Storage, so they would be automatically removed during uninstall process.

I ran across few code samples, proving me there is no easy way to do that (and I realized saving images on external memory is a piece of cake too). However, I managed to find that question: Trouble writing internal memory android and came up with something similiar. After some time testing I ran across "Unable to resume activity" exception, but fortunately I've found a tweak: image from camera intent issue in android

Now my code looks like this:

private void dispatchTakePictureIntent(int actionCode) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), FITPAL_TEMP_PICTURE_NAME);
    Uri outputFileUri = Uri.fromFile(file);

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(takePictureIntent, actionCode);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case ACTION_TAKE_PHOTO_B: {
        if (resultCode == RESULT_OK) {
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), FITPAL_TEMP_PICTURE_NAME);
            Bitmap picture = BitmapFactory.decodeFile(file.getAbsolutePath());

            if (picture == null) {
                showShortToast(R.string.problem_with_taking_a_picture);
                return;
            }

            File dir = getDir(Environment.DIRECTORY_PICTURES, Context.MODE_PRIVATE);
            File internalFile = null;

            internalFile = new File(dir, generateUniqueFilename());
            internalFile.setReadable(true);
            internalFile.setWritable(true);
            internalFile.setExecutable(true);
            try {
                internalFile.createNewFile();

                FileOutputStream fos = new FileOutputStream(internalFile);
                BufferedOutputStream bos = new BufferedOutputStream(fos);

                picture.compress(Bitmap.CompressFormat.JPEG, JPEG_FILE_QUALITY, bos);

                bos.flush();
                bos.close();

                String filePath = internalFile.getAbsolutePath();
                pictureFilePaths.add(filePath);
                addPictureThumbnail(filePath);

                Log.d("mounted?", "" + Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
                Log.d("delete test 23123123", "" + file.getAbsolutePath() + " " + file.delete());
            } catch (Exception e) {
                Log.e("eks", e.getMessage());
                showShortToast(R.string.problem_with_taking_a_picture);
            }
        }
        else if (resultCode == RESULT_CANCELED) {
            showShortToast(R.string.taking_picture_has_been_cancelled);
        }
        break;
    }
    default:
        break;
    }
}

Basically what I'm trying to do is write a picture to External Storage and move it to Internal Storage. It works in 80% of cases, it's not 100% because it sometimes just "resets" the activity - the new empty layout shows up, just as if it was just created. I have no idea what is happening. I realized that both onSaveInstanceState and onRestoreInstanceState fire in that case and I've no idea why (no exceptions are thrown along the way).

The activity that is invoking all that code is extending FragmentActivity. I've read there are some bugs that might cause that in older versions of Android Support Package, but I've just updated it to version 13 and the problem still persists.

The other thing is that the file saved on external storage won't delete - file.delete() always returns false . Is it a matter of me not having an sdcard? (I mean I have a slot in my phone, but I just have Internal Storage of 16GB). The file gets saved to /mnt/sdcard/Pictures/ folder as if it was emulated somehow.

In advance to the questions: I have both android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE permissions set:

I tried replacing Environment.getExternalStoragePublicDirectory with getCacheDir , getExternalCacheDir and none of them worked either..

I'm testing all that stuff on my Samsung Galaxy SII with Android 4.0.4 (stock)

链接地址: http://www.djcxy.com/p/67506.html

上一篇: 在App中显示PDF文件

下一篇: android将图片保存到内部存储器的麻烦