私は様々な投稿とサイトを参照しています。私は3日以来これを試しています。ImageView(カメラからキャプチャした画像)を自分の定義したフォルダ内の画像に保存します
マイアクティビティには2つのボタンがあります!最初はキャプチャ、もう1つは保存です。キャプチャボタンを押すと、カメラを起動し、画像をクリックしてOKをクリックすると、その画像がアクティビティのImageViewに設定されます。今、私が保存ボタンをクリックすると、ファイルエクスプローラに現れなければならない自分のフォルダの内部メモリに画像を保存する必要があります。 Say MyCustomFolder> Pictures> MyCapture.jpg
私はいくつかの例を試しましたが、1つのケースでは、DownloadsやDCIMやPICTURESなどのデフォルトのディレクトリに画像を保存することができました。他の場合には
、私は>写真のAndroid>データ> com.example.shravan.camera>ファイルに画像を保存することができました>私はここに投稿していただく場合にはmyImage.jpg
、私はできる午前/data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile
で画像を保存するためにここで私は、私はいつものようにするFileNotFound例外を取得していますようにコメントしているもう一つの方法セーブファイルを持っています!
ただし、自分のカスタムフォルダに保存することはできません。コードを確認して助けてください、私が何かを見逃している場合は教えてください!
コード:
MainActivity.java
package com.example.shravan.asbdbsadbsabcxscsa;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Uri imageUri;
private static final String TAG = "abc";
static final int REQUEST_IMAGE_CAPTURE =1 ;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.myIV);
}
public void Capture(View v){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
print("in onAC");
imageUri=data.getData();
iv.setImageURI(imageUri);
}
}
public void savee(View v){
Context context =this;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, "myfile");
try {
FileOutputStream fos = new FileOutputStream(fileWithinMyDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
print("path is"+fileWithinMyDir.getAbsolutePath());
fos.flush();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void saveFile(View v){
Context context =this;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File mydir = context.getFilesDir();
String filename = "Arunachala/Shravan/Images/MyImage.jpg";
File file = new File(mydir, filename);
file.mkdirs();
print("path is"+file.getAbsolutePath());
try {
FileOutputStream fos = new FileOutputStream(file);
print("path is"+file.getAbsolutePath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void print(String s){
Log.d(TAG, s);
}
}
これは私のcontent_main.xmlファイルです: content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shravan.asbdbsadbsabcxscsa.MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myIV"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:id="@+id/myB"
android:onClick="Capture"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/myIV" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/myB"
android:onClick="savee" />
</RelativeLayout>
これは私のAndroidManifest.xmlファイルです: のAndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shravan.asbdbsadbsabcxscsa">
<uses-feature android:name="android.hardware.Camera" android:required="true"></uses-feature>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
マイ最新logcat:
08-09 14:28:38.889 12727から12727/com.example.shravan .asbdbsadbsabcxscsa D/abc:パスは/ data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfileです。
助けてください!
コードを保存する:
"ファイルエクスプローラに表示する必要がある自分のフォルダ内の画像を内部メモリに保存する必要があります。MyCustomFolderと言う>画像> MyCapture.jpg" - しないでください。これは、あなたのWindowsプログラムが 'C:'ドライブ上に独自のカスタムディレクトリを必要としていると言うことと同じです。 – CommonsWare
よろしくお願いします。それは可能ではありませんか?デバイスストレージを開くと、SnapChat、WhatsAPPなどのさまざまなアプリのフォルダが表示されます。そうではありませんか? –
可能です。すべてを「C:」(1995年以降)にダンプしていたWindowsプログラムの開発者は、やっかいだったのと同じように、やっかいです。それを超えて、あなたのコードには[外部ストレージ](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html)と関係するものは何もありません。書こうとしている。おそらく 'Environment.getExternalStorageDirectory()'が必要です。また、Androidでは大文字と小文字が区別されますので、 'android.hardware.Camera'を' android.hardware.camera'に置き換えてください。 – CommonsWare