1
在我的應用程序我有一個選項,拍攝照片,將被保存在文件夾:爲什麼新創建的文件夾中的圖像沒有顯示出來?
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/my-images";
圖像會保存很好,我也有一個選項,選擇圖像使用這種代碼:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
這裏最初是用來顯示2個畫廊
1.相機
2.我的圖像
選擇任何他們等的,都能正常運作。
但沒理由我刪除了my-images文件夾並再次運行應用程序。再次創建相同的文件夾並按預期保存圖像。
的問題是:
我的圖像畫廊沒有顯示出來,現在,當我點擊選擇圖像按鈕。
除了刪除my-images文件夾並重新啓動應用程序,我沒有改變任何代碼,因爲它曾經很好。
爲什麼這樣呢?
謝謝
編輯:
我的活動代碼,允許用戶拍照並存儲在SD卡上,並瀏覽圖片庫:
public class PictureFromAppActivity extends SherlockActivity {
private File dir;
private Bitmap photo;
private String encodedString;
private InputStream is;
private ImageView imagePreview;
private String selectedImagePath;
private static final int CAMERA_REQUEST = 1888;
private static final int SELECT_PICTURE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imagePreview = (ImageView) findViewById(R.id.image_preview);
Button takePicture = (Button) findViewById(R.id.take_a_picture);
Button selectImage = (Button) findViewById(R.id.select_picture);
takePicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
selectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
if (data != null) {
photo = (Bitmap) data.getExtras().get("data");
Bitmap bitmapOrg = photo;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.PNG, 100, bao);
byte[] ba = bao.toByteArray();
try {
encodedString = Base64.encodeBytes(ba, 0);
} catch (IOException e1) {
}
if (android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED)) {
File imagesFolder = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"/my-images");
if (!imagesFolder.exists()) {
imagesFolder.mkdirs();
} else {
}
String fileName = "image_" + count + ".jpg";
f = new File(imagesFolder, fileName);
while (f.exists()) {
count++;
fileName = "image_" + String.valueOf(count)
+ ".jpg";
f = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/my-images", fileName);
}
} else {
}
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(ba);
o.flush();
fo.close();
try {
File imageFile = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ "/my-images" + "/image_" + count + ".jpg");
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
imagePreview.setImageBitmap(bitmap);
Log.d("Image Width", "+" + imagePreview.getWidth());
Log.d("Image Height", "+" + imagePreview.getHeight());
} catch (Exception e) {
}
} else {
Intent intent = new Intent(PictureFromAppActivity.this,
PictureFromAppActivity.class);
startActivity(intent);
}
} else if (requestCode == SELECT_PICTURE) {
if (data != null) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
try {
File imageFile = new File(selectedImagePath);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile
.getAbsolutePath());
imagePreview.setImageBitmap(bitmap);
} catch (Exception e) {
}
} else {
}
}
}
}
private String getPath(Uri selectedImageUri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
在哪裏我應該使用此代碼? –
您必須在打開庫以查看由您創建的圖像或文件夾時使用此代碼。這段代碼只是發送廣播刷新您的SD卡和圖書館查看您的文件夾/圖像 –
所以之前打電話打開畫廊的意圖? –