gpt4 book ai didi

java - 如何保存和显示应用程序制作的照片

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

这是一个相当大的问题,我做了很多尝试,但我就是没有得到它,而且我失败得很厉害。所以我制作了一个带有 2 个按钮菜单的应用程序。如果您单击“addpic”按钮,它将转到“FotoMaker”类,如果您单击“make_pic”按钮,您可以在那里制作图片。现在,如果用户按下“save_pic”按钮,我想保存这张图片。然后,当您单击 MenuScreen.java 中的“watch_your_picture”时,它应该调用 ShowPhoto.java 文件中调用的 watch_pic.xml。您应该会在这里看到照片。这是我已经得到的:

PhotoMaker.java:

package com.example.random;




import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
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;


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

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


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

}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
}
}

}

ShowPhoto.java:

package com.example.random;

import android.app.Activity;
import android.os.Bundle;


public class ShowPhoto extends Activity{


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


}
}

addpic.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|center_horizontal|center_vertical"
android:layout_marginTop="0dp"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="make a picture here"
android:textSize="20sp" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.45"
android:src="@drawable/ic_launcher" />

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

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

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save_pic" />

</TableRow>
</TableLayout>

</LinearLayout>

watch_pic.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="imageViewer1"
android:src="@drawable/ic_launcher" />



</LinearLayout>

字符串.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">random</string>
<string name="action_settings">Settings</string>
<string name="watch_your_picture">watch your picture here</string>
<string name="nothing_here_yet">will add something here later</string>
<string name="addpic">make picture here</string>
<string name="verlaat_app">Verlaat de app</string>
<string name="make_pic">make picture</string>
<string name="save_pic">SAVE</string>
<string name="imageView1">test is a generic contect</string>


</resources>

最佳答案

您好,您可以在 onActivityResult 中恢复图像的 uri,当您单击 watch_your_picture 时,您将图片的 uri 发送到下一个 Activity ,您可以按上述方式修改代码:

`

    package com.example.random; 




import android.app.Activity;
import android.contect.ContectValues;
import android.content.Intent;
import android.graphics.Bitmap;
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.addpic);

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 (currImageURI != 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"));
}
}
/*********************************** method to take URI of the picture *******************/
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",currImageURI.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)
/*********dimention**********************/
return Bitmap.createScaledBitmap(bitmap, 100, 100, true);
else {
return null;
}
}


}

`

关于java - 如何保存和显示应用程序制作的照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26962045/

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