我是Android的新手,想要制作一个可以从我的手机上传任何类型的文件(图像,PDF,文档等)的应用程序。此外,我想添加相机api将图像存储到我的手机图库。有人可以协助我这样做吗?
以下是从相机和图库上传图像的代码,但我在拍摄图像时遇到问题。图片未保存在图库中。
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button b1;
ImageView i1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
i1=(ImageView)findViewById(R.id.imageView);
b1.setOnClickListener(this);
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
i1.setImageBitmap(bitmap);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image from gallery......******************.........", picturePath + "");
i1.setImageBitmap(thumbnail);
}
}
}
@Override
public void onClick(View v) {
if(v==b1)
{
selectImage();
}
}
}
显示错误的代码段是:
outFile = new FileOutputStream(file);
我收到以下错误:
01-30 11:56:14.206 15924-15924/com.example.innovative.imageupload W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Phoenix/default/1422599174089.jpg: open failed: ENOENT (No such file or directory)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:416)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at com.example.innovative.imageupload.MainActivity.onActivityResult(MainActivity.java:94)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:5472)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3406)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3453)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.access$1200(ActivityThread.java:150)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5283)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
01-30 11:56:14.276 15924-15924/com.example.innovative.imageupload W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
01-30 11:56:14.286 15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.Posix.open(Native Method)
01-30 11:56:14.286 15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
01-30 11:56:14.286 15924-15924/com.example.innovative.imageupload W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:400)
01-30 11:56:14.286 15924-15924/com.example.innovative.imageupload W/System.err﹕ ... 16 more
我无法找到图片未保存到图库的错误。请帮助我这样做。 在此先感谢。
答案 0 :(得分:0)
如果您使用sdk&gt; = 23,并且不使用外部存储,那么这将显示问题,我有同样的问题,您确定您使用该活动中的权限 //为了安全起见,您应该检查SD卡是否已安装 //在执行此操作之前使用Environment.getExternalStorageState()。
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"SARApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d("SARCamera", "failed to create directory");
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
}