在我的项目中,我无法将图像保存在所需的文件夹中。我检查了生成的路径 / storage / emulated / 0 / myImages ,同时调试不存在。我没理解为什么没有创建 myImages 文件夹。我已将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
和READ_EXTERNAL_STORAGE
添加到我的 AndroidManifest.xml 。这是我的错误日志:
W / System.err:java.io.IOException:没有这样的文件或目录 W / System.err:at java.io.UnixFileSystem.createFileExclusively0(本机方法) W / System.err:at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281) W / System.err:at java.io.File.createNewFile(File.java:1000) W / System.err:at com.stegano.strenggeheim.fragment.FragmentEncode.saveImage(FragmentEncode.java:170) W / System.err:at com.stegano.strenggeheim.fragment.FragmentEncode.onActivityResult(FragmentEncode.java:136) W / System.err:at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) W / System.err:at android.app.Activity.dispatchActivityResult(Activity.java:7235) W / System.err:at android.app.ActivityThread.deliverResults(ActivityThread.java:4320) W / System.err:at android.app.ActivityThread.handleSendResult(ActivityThread.java:4367) W / System.err:在android.app.ActivityThread.-wrap19(未知 资料来源:0)W / System.err:at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1649) W / System.err:at android.os.Handler.dispatchMessage(Handler.java:105)W / System.err:
在android.os.Looper.loop(Looper.java:164)W / System.err:at android.app.ActivityThread.main(ActivityThread.java:6541) W / System.err:at java.lang.reflect.Method.invoke(Native Method) W / System.err:at com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的Java代码:
package com.stegano.strenggeheim.fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.stegano.strenggeheim.R;
import com.stegano.strenggeheim.utils.PermissionUtility;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
public class FragmentEncode extends Fragment {
private static final String IMAGE_DIRECTORY = "/myImages";
private static final int GALLERY = 0, CAMERA = 1;
private static Uri contentURI;
TextView imageTextMessage;
ImageView loadImage;
public FragmentEncode() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public void galleryIntent() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY);
}
private void cameraIntent() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_encode, container, false);
imageTextMessage = view.findViewById(R.id.imageTextMessage);
loadImage = view.findViewById(R.id.loadImage);
loadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPictureDialog();
}
});
return view;
}
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(getContext());
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera",
"Cancel"
};
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
galleryIntent();
break;
case 1:
cameraIntent();
break;
case 2:
dialog.dismiss();
break;
}
}
});
pictureDialog.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
contentURI = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), contentURI);
String path = saveImage(bitmap);
Log.println(Log.INFO, "Message", path);
Toast.makeText(getContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
loadImage.setImageBitmap(bitmap);
imageTextMessage.setVisibility(View.INVISIBLE);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Failed!", Toast.LENGTH_SHORT).show();
}
}
} else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
loadImage.setImageBitmap(thumbnail);
saveImage(thumbnail);
Toast.makeText(getContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
imageTextMessage.setVisibility(View.INVISIBLE);
}
}
public String saveImage(Bitmap bmpImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmpImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
File encodeImageDirectory =
new File(Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
if (!encodeImageDirectory.exists()) {
encodeImageDirectory.mkdirs();
}
try {
String uniqueId = UUID.randomUUID().toString();
File f = new File(encodeImageDirectory, uniqueId + ".png");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(getContext(),
new String[]{f.getPath()},
new String[]{"image/png"}, null);
fo.close();
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
}