我创建了一个Android应用程序,它将捕获图片并保存在sdcard文件夹中,现在我想用自定义名称保存图像。
import java.io.ByteArrayOutputStream;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView) this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, "new-photo-name.jpg");
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
MediaStore.Images.Media.insertImage(getContentResolver(), photo,
null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
}
}
这里是用于捕获图像并将其保存在SD卡文件夹中的代码,请帮我保存具有特定名称的图像,例如:android.jpeg
答案 0 :(得分:1)
File outFile = new File(Environment.getExternalStorageDirectory(), "myname.jpeg");
FileOutputStream fos = new FileOutputStream(outFile);
photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
您还需要在Android Manifest中添加Permission。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
该代码段会将photo
的内容保存在/sdcard
内,名称为"myname.jpeg"
答案 1 :(得分:0)
您需要将fileName作为Intent Extras -
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, "android.jpg");
答案 2 :(得分:0)