gpt4 book ai didi

android - 如何从应用小部件打开相机和图库的 Intent 选择器

转载 作者:太空狗 更新时间:2023-10-29 15:25:51 26 4
gpt4 key购买 nike

我创建了一个主屏幕小部件 (1*1),并尝试从该小部件打开相机和图库的 Intent 选择器。我尝试从其他类打开 Intent 选择器,但它不起作用。这是我的配置 Activity 中的代码:

Intent clickIntent = new Intent(ConfigurationActivity.this, WidgetProviderSmall.class);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

PendingIntent pendingIntent = PendingIntent.getBroadcast(ConfigurationActivity.this, mAppWidgetId, clickIntent, 0);
views.setOnClickPendingIntent(R.id.img_widget, pendingIntent);
appWidgetManager.updateAppWidget(mAppWidgetId, views);

这是来自我的 AppWidgetProvider 类:
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction()==null) {
Bundle extras = intent.getExtras();
if(extras!=null) {
class.OpenIntentChooser();
}
}
else {
super.onReceive(context, intent);
}
}

有什么建议么?

最佳答案

对于相机代码,请根据您的需要使用

import android.app.Activity;  
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}


}

Gallery Code请根据需要修改
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;

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

public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data

Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageView imgView = (ImageView) findViewById(R.id.imgView);
// Set the Image in ImageView after decoding the String
imgView.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));

} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}

}

}

在 AndroidManifest.xml 中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 如何从应用小部件打开相机和图库的 Intent 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038631/

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