gpt4 book ai didi

android - 如何将图片放入应用程序的内存中?

转载 作者:行者123 更新时间:2023-11-29 14:30:34 27 4
gpt4 key购买 nike

我正在尝试从相机应用程序拍摄照片并将其保存到应用程序的文件中,搜索到现在让我找到了一些关于内容 Uris 的内容。(我不知道它们是什么或如何使用它们)。我已经能够在外部存储器中保存照片,所以如果有办法将照片从那里移动到 apk 文件中,那就太好了。

我想放照片的路径是“appname/app/src/main/assets/photos”

这是我现在的代码:

public class Add_Comment_Picture extends AppCompatActivity {
static final int CAM_REQUEST = 1;
ImageView imageView;
ImageButton button;
private static final int REQUEST_CODE = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__comment__picture);
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
button = (ImageButton) findViewById(R.id.imageButtonCamera);
imageView = (ImageView) findViewById(R.id.imageView);

button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent , CAM_REQUEST);
}
});


}

@Override
protected void onStart() {
super.onStart();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);

List<String> permissions = new ArrayList<String>();
if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}

if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}

if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.CAMERA);
}
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
}
}


}

private File getFile()
{
File folder = new File("sdcard/camera_app");
if (!folder.exists())
{
folder.mkdir();
}
File image_file = new File(folder,"cam_image.jpg");
return image_file;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "sdcard/camera_app/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
super.onActivityResult(requestCode, resultCode, data);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 111: {
for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);


} else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);

}
}
}
break;
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
}

最佳答案

您的 apk 文件类似于 android 应用程序安装文件。它是一个压缩文件,您也可以使用 7-Zip 或类似程序打开它。当您在设备上安装应用程序时,该文件将被提取到您的内存中。

URI 代表统一资源标识符。它标识您的资源,在您的情况下是拍摄的照片。

您可以将图片保存到内部或外部存储器中。如果你想使用外部存储器,你需要权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

如果您将文件保存到内存中,其他应用程序将看不到它,并且您无法使用 af 文件资源管理器找到它。如果它是外部的,您将在文件资源管理器中找到它。

在 Stackoverflow 上查看这个关于如何保存相机文件的答案:

Stackoverflow Camera Intent

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Then, in onActivityResult, replace data.getData() with fileUri.toString() and it will solve your problem.

关于android - 如何将图片放入应用程序的内存中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37292674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com