私はアンドロイドが初めてで、携帯電話からあらゆる種類のファイル (画像、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
画像がギャラリーに保存されない理由を見つけることができません。これを行うのを手伝ってください。前もって感謝します。