gpt4 book ai didi

java - 如何在一个类(class)中显示我在另一个类(class)中保存的图片

转载 作者:行者123 更新时间:2023-12-01 12:16:31 25 4
gpt4 key购买 nike

我有一个可以制作和保存图片的应用程序。现在我当这张图片在另一个类(class)显示时。我刚刚开始用java编程,不知道如何做到这一点。

FotoMaker.java:

package com.example.random;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class FotoMaker extends Activity
{
ImageView iv;
Uri uriOfPicture;


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.addpictolist);

iv = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{


@Override
public void onClick (View v){
takeImageFromCamera();

}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap image=createBitmapFromURI(uriOfPicture);
iv.setImageBitmap(image);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (uriOfPicture != null) {
outState.putString("cameraImageUri", uriOfPicture.toString());
}
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey("cameraImageUri")) {
uriOfPicture = Uri.parse(savedInstanceState.getString("cameraImageUri"));
}
}
public void takeImageFromCamera(){
String fileName = "new-photo-name.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");

uriOfPicture = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Log.i("uri",uriOfPicture.toString());
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriOfPicture);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,
0);


}

public Bitmap createBitmapFromURI(Uri uri) {

Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getApplicationContext()
.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (bitmap != null)
return Bitmap.createScaledBitmap(bitmap, 100, 100, true);
else {
return null;
}
}


}

ShowPhoto.java:

package com.example.random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;


public class ShowPhoto extends Activity{

ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_pic);

iv = (ImageView) findViewById(R.id.imageView1);

}
}

最佳答案

获得 URI 后,您可以将其传递给新 Activity 。因此,在 FotoMaker 的 onActivityResult 中,只需启动 ShowPhoto Activity 并额外发送 uriOfPicture。

Intent intent = new Intent(this, ShowPhoto.class);
intent.putExtra("uri", uriOfPicture);
startActivity(intent);

在您的 ShowPhoto Activity 中,只需捕获参数并显示它:

String uriOfPicture = getIntent().getStringExtra("uri");
Bitmap image = createBitmapFromURI(uriOfPicture);
iv.setImageBitmap(image);

我假设您的大部分代码已经可以运行。

关于java - 如何在一个类(class)中显示我在另一个类(class)中保存的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963710/

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