public class CameraActivity extends Activity {
ImageView imgFavorite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
// set listener on image view
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// open cam
open();
}
});
}
public void open(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//we will use this function startActivityForResult() to launch this activity and wait for its result.
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgFavorite.setImageBitmap(bp);
}
这是我使用现有相机应用程序捕获图像的代码..但它在某些Android设备上不起作用..它不能在库中保存捕获图像..我怎样才能解决这个问题?
答案 0 :(得分:0)
还使用此权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
使用此:
ImageView drawingVIew = (ImageView) findViewById(R.id.drawing);//let this be your image view
//your other code
drawingVIew.setDrawingCacheEnabled(true);
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), drawingVIew.getDrawingCache(),
UUID.randomUUID().toString() + ".png", "drawing");
if (imgSaved != null) {
Toast savedToast = Toast.makeText(getApplicationContext(),
"Drawing saved to Gallery!", Toast.LENGTH_SHORT);
savedToast.show();
} else {
Toast unsavedToast = Toast.makeText(getApplicationContext(),
"Oops! Image could not be saved.", Toast.LENGTH_SHORT);
unsavedToast.show();
}
drawingVIew.destroyDrawingCache();
答案 1 :(得分:0)
是的,你可以压缩文件。它取决于你使用的百分比。你应该得到你想要的结果的最佳数字。就像我在下面的代码中使用100
try {
FileOutputStream fos = new FileOutputStream(yourImage);
//here change the value from 100 to your best result of compressing
the photo before saving . and this will also update the gallery
newImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
//refreshing gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(yourImage));
sendBroadcast(mediaScanIntent);
} catch (IOException e) {
Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
return;
}