1

ImageViewに表示される(そして内部でさらにいくつかの作業を行う)写真をキャプチャするアプリケーションがあります。

カメラを開いたときに問題が発生し、写真をキャプチャした後、保存と破棄のオプションが表示されます。保存ボタンをクリックすると、カメラモードに戻ります。

電話のメモリをクリーンアップする場合(タスクマネージャ->> RAM->>メモリのクリア)、この問題は発生しません。

問題は、十分なメモリがない場合にカメラインテントが結果を出さないことです。しかし、通常のカメラを開くと、写真がキャプチャされます。

この問題をどのように解決するか。1つの解決策は、アプリケーションを起動する前にメモリをクリーンアップするたびにここにあります。

しかし、これは正しい方法ではありません。なぜなら、私が私のアプリケーションを一般の人々に紹介するとき、彼らはこれらのことをしないからです。

今、私はここで何をする必要がありますか?

アプリケーションでメモリをクリーンアップする方法はありますか?アプリケーションで使用されている最小メモリまたは最大メモリを知る方法。それで、最初に私のアプリケーションチェックは、そこに、それだけのメモリが利用可能かどうかを確認します。利用可能な場合は続行し、そうでない場合は、メモリをクリーンアップ(または一部のアプリケーションをアンインストール)するようにユーザーに指示します。

アプリケーションで次のコードを使用しています。

レイアウト->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button 
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/photo"
    />

<ImageView 
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/button1"
    android:contentDescription="@string/image"/>
</RelativeLayout>

アクティビティ->>

public class TestCameraActivity extends Activity {

private Button button;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_camera);

    button = (Button) findViewById(R.id.button1);
    imageView = (ImageView) findViewById(R.id.image);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, 100);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100) {
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_test_camera, menu);
    return true;
}

}
4

3 に答える 3

3

あなたのコードは、私のために働いたコードの下で試してみてください

File root = new File(Environment.getExternalStorageDirectory(), "Directory");
        if (!root.exists()) {
            root.mkdirs();
        }
         File file = new File(root,"filename.jpeg" );
         outputFileUri = Uri.fromFile( file );

         Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    photoPickerIntent.putExtra("return-data", true);
   startActivityForResult(photoPickerIntent,100);

OnActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{   

    switch(requestCode)
     { 

     case 100:
     if(resultCode == RESULT_OK)
     { 

           if(data==null)
            {
               Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                         imageView.setImageBitmap(bitmap);
            }
         break;
     }
}
}
于 2012-11-16T07:18:33.617 に答える
2

CameraIntentを適切に呼び出していない可能性があります。以下のコードを試して、機能することを期待してください。

CameraIntent

String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+filename;
System.out.println("thumbnail path~~~~~~"+filepath);
File file = new File(filepath);
Uri outputFileUri = Uri.fromFile(file);

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 100);

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100) {
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        Bitmap bitmap;
        bitmap=GlobalMethods.decodeFile(_path);//function to decode.
        if (bitmap == null) {
            ivPhy.setImageBitmap(bitmap);
        } 
        else {
            ivPhy.setImageBitmap(bitmap);
            ivPhy.setScaleType(ScaleType.FIT_XY);
        }
    }


}

Manifest Permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
于 2012-11-22T05:11:07.253 に答える
0

この問題の解決策をまだ探している人へ:

許可の欠如は、この問題の背後にある主な理由の1つです。この権限をマニフェストファイルに追加してください。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
于 2014-09-16T19:48:28.983 に答える